package com.ahstu.learning.learning;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ahstu.learning.R;
import com.ahstu.learning.SubjectActivity.BaseCourseActivity;
import com.ahstu.learning.SubjectActivity.Biology;
import com.ahstu.learning.SubjectActivity.Chinese;
import com.ahstu.learning.SubjectActivity.Chymist;
import com.ahstu.learning.SubjectActivity.CourseAdapter;
import com.ahstu.learning.SubjectActivity.English;
import com.ahstu.learning.SubjectActivity.Geography;
import com.ahstu.learning.SubjectActivity.History;
import com.ahstu.learning.SubjectActivity.Math;
import com.ahstu.learning.SubjectActivity.Physical;
import com.ahstu.learning.SubjectActivity.Politics;
import com.ahstu.learning.SubjectActivity.Subject;
import com.ahstu.learning.player.VodActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
public class Course_list extends BaseCourseActivity {
    
    ListView listView;
    LinearLayout progressBar;
    ArrayList<Subject> sublist=new ArrayList<>();
    Context context;
    ImageView backButton;
    private CourseAdapter adapter;
    private TextView course_down_list;
    private String course_type;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_list);
        initBaseActivity(true, false);
        init();
        
    }

    void init() {
        course_type = getIntent().getStringExtra("subject");

        course_down_list = (TextView) findViewById(R.id.course_down_list);
        course_down_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File huancun = new File("/sdcard/diandixuetang/" + course_type + "/");
                if (huancun.exists() && huancun.isDirectory()) {
                    if (huancun.list().length > 0) {
                        Intent intent = new Intent(Course_list.this, FileManager.class);
                        intent.putExtra("subject", course_type);
                        startActivity(intent);
                    } else
                        Toast.makeText(Course_list.this, "缓存列表为空", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(Course_list.this, "缓存列表为空", Toast.LENGTH_SHORT).show();


            }
        });

        backButton=(ImageView)findViewById(R.id.course_backbutton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        context=this;
        progressBar=(LinearLayout) findViewById(R.id.progress);
        listView=(ListView)findViewById(R.id.listview);
        progressBar.setVisibility(View.VISIBLE);
        if (course_type.equals("Chinese"))
        {
            BmobQuery<Chinese> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Chinese>() {
                @Override
                public void onSuccess(List<Chinese> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);
                        
                    }

                    listView.setAdapter(new CourseAdapter(sublist,context));
                    
                    
                }

                @Override
                public void onError(int i, String s) {
                    Toast.makeText(Course_list.this,"查询失败",Toast.LENGTH_SHORT).show();
                }
            });


        } else if (course_type.equals("Math"))
        {
            BmobQuery<Math> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Math>() {
                @Override
                public void onSuccess(List<Math> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });

        } else if (course_type.equals("English"))
        {
            BmobQuery<English> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<English>() {
                @Override
                public void onSuccess(List<English> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });


        } else if (course_type.equals("Physical"))
        {
            BmobQuery<Physical> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Physical>() {
                @Override
                public void onSuccess(List<Physical> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        } else if (course_type.equals("Chymist"))
        {
            BmobQuery<Chymist> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Chymist>() {
                @Override
                public void onSuccess(List<Chymist> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });


        } else if (course_type.equals("Biology"))
        {
            BmobQuery<Biology> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Biology>() {
                @Override
                public void onSuccess(List<Biology> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });

        } else if (course_type.equals("Politics"))
        {
            BmobQuery<Politics> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Politics>() {
                @Override
                public void onSuccess(List<Politics> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });


        } else if (course_type.equals("History"))
        {
            BmobQuery<History> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<History>() {
                @Override
                public void onSuccess(List<History> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        } else if (course_type.equals("Geography"))
        {

            BmobQuery<Geography> query=new BmobQuery<>();
            query.findObjects(Course_list.this, new FindListener<Geography>() {
                @Override
                public void onSuccess(List<Geography> list) {
                    progressBar.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < list.size(); i++) {
                        Subject subject = new Subject();
                        subject.setCoursename(list.get(i).getCoursename());
                        subject.setImageurl(list.get(i).getImage());
                        subject.setPlayId(list.get(i).getPlayId());
                        subject.setPlayUrl(list.get(i).getPlayUrl());
                        subject.setCourseinfo(list.get(i).getCourseinfo());
                        subject.setObjectid(list.get(i).getObjectId());
                        subject.setCommemt(list.get(i).getComment());
                        sublist.add(subject);

                    }
                    listView.setAdapter(new CourseAdapter(sublist,context));
                }

                @Override
                public void onError(int i, String s) {

                }
            });

        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int j, long l) {
                
                Intent intent=new Intent(Course_list.this, VodActivity.class);
                intent.putExtra("objectid", sublist.get(j).getObjectid());
                intent.putExtra("playId", sublist.get(j).getPlayId());
                intent.putExtra("playUrl", sublist.get(j).getPlayUrl());
                intent.putExtra("course_name", sublist.get(j).getCoursename());
                intent.putExtra("course_info", sublist.get(j).getCourseinfo());
                Subject subject = new Subject();
                subject.setCourse_commemt_list(sublist.get(j).getCommemt());
                subject.setCourse_number(course_type);
                subject.setCourse_title(sublist.get(j).getCoursename());
                startActivity(intent);
            }
        });
        
        
        
        
        
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //unregisterReceiver(receiver);

    }
}
